<?php

class Persona_model extends CI_Model
{

    function __construct()
    {
        parent::__construct();
        $this->tablename = 'users';
    }
    function buscarcliente($idUsuario,$tipPer,$ruc,$razon,$docu,$nombs,$apellidos){
        $cad="SELECT per.IdPersona as codPer,per.TipoDePersona as tipo, per.RUC as ruc, per.RazonSocial as razon, per.Nombres as nomb, per.Apellidos as apellido,
            per.DNI as dni from Persona per, Cliente clie where per.IdPersona=clie.IdCliente and clie.Usuario_IdUsuario=".$idUsuario;
        if ($ruc != "") {
            $cad = $cad . " AND per.RUC = $ruc";
        }
        if ($tipPer != "" && $tipPer != "Todos") {
            $cad = $cad . " AND per.TipoDePersona = '$tipPer'";
        }
        if ($razon != "") {
            $cad = $cad . " and per.RazonSocial like '%$razon%' ";
        }
        if ($docu != "") {
            $cad = $cad . " and per.DNI like '%$docu%' ";
        }
        if ($nombs != "") {
            $cad = $cad . " and per.Nombres like '%$nombs%' ";
        }
        if ($apellidos != "") {
            $cad = $cad . " and per.Apellidos like '%$apellidos%' ";
        }
        return $this->db->query($cad)->result_array();;
    }
    function buscarUser($estado,$tipPer,$ruc,$razon,$docu,$nombs,$apellidos){
        $cad="SELECT per.Email as email,per.IdPersona as codPer,per.TipoDePersona as tipo, per.RUC as ruc, per.RazonSocial as razon, per.Nombres as nomb, per.Apellidos as apellido,user.Estado as est,
            per.DNI as dni from Persona per, usuario user where per.IdPersona=user.IdUsuario and user.IdUsuario<>1";
        if ($ruc != "") {
            $cad = $cad . " AND per.RUC = $ruc";
        }
        if ($estado != "" && $estado != "Todos") {
            $cad = $cad . " AND user.Estado = '$estado'";
        }
        if ($tipPer != "" && $tipPer != "Todos") {
            $cad = $cad . " AND per.TipoDePersona = '$tipPer'";
        }
        if ($razon != "") {
            $cad = $cad . " and per.RazonSocial like '%$razon%' ";
        }
        if ($docu != "") {
            $cad = $cad . " and per.DNI like '%$docu%' ";
        }
        if ($nombs != "") {
            $cad = $cad . " and per.Nombres like '%$nombs%' ";
        }
        if ($apellidos != "") {
            $cad = $cad . " and per.Apellidos like '%$apellidos%' ";
        }
        return $this->db->query($cad)->result_array();;
    }
    function buscarUsuario($idUsuario){
        $cad="select p.RazonSocial as razon, p.Direccion as direc, p.Telefono as telf, p.Rublo as rubl,
        p.Email as email, p.Nombres as nom, p.Apellidos as ape, p.DNI as dni, p.TelfCel as telcel, 
        p.Genero as gen,
        p.TipoDePersona as tip, p.RUC as ruc, u.Password as pass     
        from persona p, usuario u where p.IdPersona=u.IdUsuario and p.IdPersona=$idUsuario";
        return $this->db->query($cad)->result_array();;
    }
    function actualizarPer($per,$user,$Cond){
        $this->db->where('IdPersona', $Cond);
        $this->db->update('persona', $per); 
        $this->db->where('IdUsuario', $Cond);
        $this->db->update('usuario', $user); 
    }
    
 }
?>
